<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="品牌名称：">
            <el-input v-model="listQuery.queryObject.brandName" class="input-width" placeholder="品牌名称" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="flashTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="200" align="center">
          <template slot-scope="scope">{{scope.row.brandCode}}</template>
        </el-table-column>
        <el-table-column label="品牌名称" align="center" width="200">
          <template slot-scope="scope">{{scope.row.brandName}}</template>
        </el-table-column>
                <el-table-column label="品牌名称" align="center" width="200">
          <template slot-scope="scope">{{scope.row.productName}}</template>
        </el-table-column>
          <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="handleSelectSession(scope.row)">产品维护
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
   
  </div>
</template>
<script>
  import {fetchList} from '@/api/partner';
   const cityOptions = [];
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 5,
    queryObject: {brandName:""}
  };
  const defaultFlashPromotion = {
    id: null,
    title: null,
    startDate: null,
    endDate: null,
    status: 0
  };
  export default {
    name: 'flashPromotionList',
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        goodsdialogVisible:false,
        brand: Object.assign({}, defaultFlashPromotion),
        isEdit: false, 
        product:{},
        productOptions:[] ,
        selectBrand:{},

         checkAll: false,
        checkedGoods: [],
        goodsvoList: cityOptions,
        isIndeterminate: true

      }
   
    },
    created() {
      this.getList();
    },
    filters: {
      formatActiveStatus(row) {
        // let nowTime = new Date().getTime();
        // if (nowTime >= row.startDate && nowTime <= row.endDate) {
        //   return '活动进行中';
        // } else if (nowTime > row.endDate) {
        //   return '活动已结束';
        // } else {
        //   return '活动未开始';
        // }
      },
      formatDate(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    methods: {
       handleCheckAllChange(val) {
        // this.checkedGoods = val ? cityOptions : [];
        // debugger
        if(val==true){
          this.goodsvoList.forEach((item) => {
   this.checkedGoods.push(item.goodsCode);
})
        }else{
          this.checkedGoods=[]
        }
        this.isIndeterminate = false;
      },
      handlecheckedGoodsChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.goodsvoList.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.goodsvoList.length;
      },
      handleResetSearch() {
        this.listQuery.queryObject.brandName=""
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
       getAllProducts().then(response=>{
          if(response.code=='200'){  this.productOptions=response.data}       
        })
      },
      handleShowSessionList() {
        this.$router.push({path: '/sms/flashSession'})
      },
      handleStatusChange(index, row) {
        this.$confirm('是否要修改该状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus(row.id, {status: row.status}).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消修改'
          });
          this.getList();
        });
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该活动?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteFlash(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.flashPromotion = Object.assign({},row);
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          createbrand(this.brand).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          
        }
                 
        )},

           handleAddBrandGoodsDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
      var tem={}
          tem.productCode = this.product.productCode
          tem.brandCode = this.selectBrand.brandCode
          tem.goodsCodeList = this.checkedGoods

          updateBrandGoods(tem).then(response => {
            this.goodsdialogVisible=false
                   this.productOptions=[]
        this.product={}
        this.goodsvoList=[];
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          
        }
                 
        )},

        
      handleSelectSession(row){
        this.$router.push({
          path:'/cbkhc/editProduct',
          query: {
            brandCode: row.brandCode,
            brandName: row.brandName,
            productCode: row.productCode,
            productName: row.productName,
            partnerCode: row.partnerCode,
            partnerName: row.partnerName
          }
      })
      },
      choseProduct(val){
        if(val==undefined){
          return
        }
       var tem={}
       tem.productCode=val
       tem.brandCode=this.selectBrand.brandCode;
      brandAddProductCheckbox(tem).then(response=>{ 
        this.checkedGoods=[];
        if(response.code=='200') {
        this.goodsvoList=response.data  
          this.goodsvoList.forEach((item) => {
            if(item.check=='1'){
        this.checkedGoods.push(item.goodsCode);
        }
})

          
        }
      })
      },

      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.listLoading = false
          if (response.data) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            if (typeof response.msg === 'string') {
              this.$message({
                message: response.msg,
                type: 'error'
              })
            }
          }
        });
      }
    }
  }
</script>
<style></style>


